// Get the path of the currently running do-file
local do_file_path `c(pwd)'

// Change the working directory to the location of the do-file
cd "`do_file_path'"

**
** process output from Google Earth Engine
**

** merge area
clear
import delimited "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_ee_area.csv", varnames(1) clear
keep ee_area_km2 objectid 
rename ee_area_km2 area_ee
label variable area_ee "Area (km2) : EE"
save "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_ee_area.dta", replace

clear
import delimited "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_sum_burned_area_ts2001_2008.csv", varnames(1) clear 
* shape_area shape_leng x y geo id v98
drop system 
foreach v of varlist v2-v97 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' burn`newname'
}
save "proc_data\burned_area_ts2001_2008.dta", replace

import delimited "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_sum_burned_area_ts2009_2015.csv", varnames(1) clear 
*shape_area shape_leng x y geo id v86
drop system v86 
foreach v of varlist v2-v85 {
   local x : variable label `v'
   local newname = substr("`x'", 1, 6)
   recode `v' (-999=.)
   rename `v' burn`newname'
}
save "proc_data\burned_area_ts2009_2015.dta", replace

clear 
use "proc_data\burned_area_ts2009_2015.dta" 

merge 1:1 objectid using "proc_data\burned_area_ts2001_2008.dta"
drop _merge

* many objectid year
merge m:1 objectid using "local\ee_data\burned_area\lake_chad_ntl_ctr_x_d01_ee_area.dta"
drop _merge

drop geo

** panel
reshape long burn, i(objectid) j(yearmo) 

replace burn=0 if burn==.
g year = int(yearmo/100)
g month = mod(yearmo,100)

keep if year >= 2001 & year <= 2015
sum year
*drop yearmo
drop v98

* do not include country identifiers! 
collapse (sum) burn area_ee, by(objectid yearmo year month gid_0)

* crop calendar dummies by country
*FAO GIEWS crop calendar for Chad Maize, Millet, Sorghum

* tcd some variation across millet, maize, rice, sorghum
g burn_sowgrow = inrange(month, 5, 8) if gid_0 == "TCD"
g burn_harvest = inrange(month, 9, 12) if gid_0 == "TCD"
g burn_landprp = inrange(month, 1, 4) if gid_0 == "TCD"

* except rice
replace burn_sowgrow = inrange(month, 6, 8) if gid_0 == "NER"
replace burn_harvest = inrange(month, 9, 11) if gid_0 == "NER"
replace burn_landprp = inrange(month, 1, 5) if gid_0 == "NER"
replace burn_landprp = 1 if month==12 & gid_0 == "NER"

* cassave (south)
* irr. rice
* maize north
* maize second
* maize south
* millet 
* rainfed
* sorghum
* yams
replace burn_landprp = inrange(month, 2, 4) if gid_0 == "NGA"
replace burn_sowgrow = inrange(month, 5, 8) if gid_0 == "NGA"
replace burn_harvest = inrange(month, 9, 12) if gid_0 == "NGA"
replace burn_harvest = 1 if month==1 & gid_0 == "NGA"

* maize, millet sorghum
replace burn_sowgrow = inrange(month, 5, 9) if gid_0 == "CMR"
replace burn_harvest = inrange(month, 10, 11) if gid_0 == "CMR"
replace burn_landprp = inrange(month, 1, 4) if gid_0 == "CMR"
replace burn_landprp = 1 if month==12 & gid_0 == "CMR"

label variable objectid "Seq id per grid or split grid at country border"
*label variable gid_0 "Country code from admin shp : GADM v3.6"
label variable burn "Sum of burned area over 1 month : MOD EE"
label variable area_ee "Area (km2) : EE"

label variable burn_harvest "Dummy harvest season derived : FAO GIEWS crop calendar"
label variable burn_landprp "Dummy land prep season derived : FAO GIEWS crop calendar"
label variable burn_sowgrow "Dummy sowgrowing season derived : FAO GIEWS crop calendar"

save "proc_data\BURN_mo_ts.dta", replace